// pages/square/partyBm.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "我要报名", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl: ''
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    contactId: 1,
    name: '',
    //手机/微信号
    contact: '',
    //留言
    partPro: '',
    noteMaxLen: 40,

    authData: {
      authType: '0', //0表示使用登录弹窗  1表示使用完善昵称头像弹窗 2表示直接弹出头像昵称选择框
      authUsername: "false", //是否已经授权头像昵称
    },
    showLogin: false, //是否显示授权弹窗
    showWsInfoDom: false, //直接显示头像昵称弹窗
    showPrivacyFlag: app.globalData.showPrivacyFlag,

    openid: '',
    is_nickAndHead: 0,
    shareTitle: '',
    shareImg: '',
    flag: 0
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  //选择联系方式
  contact(e) {
    let that = this;
    let id = e.currentTarget.dataset.id
    that.setData({
      contactId: id
    })
  },
  nameIpt(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //手机/微信号码
  contactIpt(e) {
    this.setData({
      contact: e.detail.value
    })
  },
  //留言
  partProVal(e) {
    this.setData({
      partPro: e.detail.value
    })
  },
  //描述输入框
  partPro: function (e) {
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > this.data.noteMaxLen) return;
    this.setData({
      currentNoteLen: len //当前字数
    })
  },


  bm() {
    let that = this;

    //判断是否授权过头像昵称了
    if (that.data.is_nickAndHead == 0) {
      that.addWxInfo();
      return;
    }

    let contactId = that.data.contactId;
    let name = that.data.name;
    let contact = that.data.contact;
    let partPro = that.data.partPro;
    if (contactId == -1) {
      app.showTip("请选择联系方式");
      return;
    }
    if (name == '') {
      app.showTip("请填写真实姓名");
      return;
    }
    if (contact == '') {
      app.showTip("请填写联系方式");
      return;
    }

    if (contactId == 1 && (isNaN(contact) || contact.length != 11)) {
      //选择了电话
      app.showTip("请填写11位数字的手机号码");
      return;
    }


    if (that.data.flag == 0) {
      that.setData({
        flag:1
      });
      wx.showLoading({
        title: '数据提交中...'
      });

      wx.request({
        url: that.data.baseUrl + 'addGameBm.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          id: that.data.id,
          contact_type: contactId,
          contact_number: contact,
          content: partPro,
          name: name
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.status == 1) {
            //后台请求成功
            app.showTip(res.data.msg);
            setTimeout(function () {
              wx.redirectTo({
                url: that.data.defaultData.goUrl
              });
            }, 1500)
          } else {
            //后台请求失败
            app.showTip(res.data.msg);
          }
          that.setData({
            flag:0
          });
        },
        fail: function (res) {
          wx.hideLoading();
          that.setData({
            flag:0
          });
        }
      });
    }
  },

  //处理完善信息
  addWxInfo() {
    this.setData({
      ['authData.authType']: 1,
      showLogin: true
    });
  },

  //接收子组件电话授权及头像昵称授权后后返回的值
  returnVal(e) {
    //授权完头像昵称
    this.setData({
      is_nickAndHead: e.detail.is_nickAndHead
    });

    app.showTip('个人信息已完善');
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var id = options.id;
    if (id == null || id == 0 || isNaN(id) || id == undefined) {
      app.showTip('抱歉,未获取到组局id哦');
      setTimeout(function () {
        wx.switchTab({
          url: '/pages/square/index'
        });
      }, 2000);
      return;
    } else {
      that.setData({
        id: id
      });
    }

    //判断缓存里是否有值
    var headerUrlList = wx.getStorageSync('headerUrlList');
    if (headerUrlList.constructor == Array && headerUrlList.length > 0) {
      //返回中有值
      that.setData({
        ['defaultData.goUrl']: headerUrlList[headerUrlList.length - 1]
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getPartBmPageInfo(res.data.openid, id);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getPartBmPageInfo(openid, id);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getPartBmPageInfo(openid, id) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getPartBmPageInfo.php',
      method: 'POST',
      data: {
        openid: openid,
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
            is_nickAndHead: res.data.is_nickAndHead,
            name: res.data.name,
            contact: res.data.tel
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1300)
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1300)
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //更新showPrivacyFlag
    this.setData({
      showPrivacyFlag: app.globalData.showPrivacyFlag
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/square/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})